package com.maddox.relax.model;

import androidx.collection.ArraySet;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.Set;

/**
 * 文件的类型
 * 包括常见的图片和视频类型
 */
public enum MimeType {
    //====================images==========
    JPEG("image/jpeg", arraySetOf(
            "jpeg",
            "jpg")),
    PNG("image/png", arraySetOf("png")),
    GIF("image/gif", arraySetOf("gif")),
    BMP("image/x-ms-bmp", arraySetOf("bmp", "x-ms-bmp")),
    WEBP("image/webp", arraySetOf("webp")),

    //===================videos=============
    MPEG("video/mpeg", arraySetOf("mpeg", "mpg")),
    MP4("video/mp4", arraySetOf("mp4")),
    QUICKTIME("video/quicktime", arraySetOf("mov", "quicktime")),
    THREEGPP("video/3gpp", arraySetOf("3gp", "3gpp")),
    MKV("video/x-matroska", arraySetOf(
            "mkv", "x-matroska"
    )),
    WEBM("video/webm", arraySetOf(
            "webm"
    )),
    TS("video/mp2ts", arraySetOf(
            "ts", "mp2ts"
    )),
    AVI("video/avi", arraySetOf(
            "avi"
    ));


    private final String mimeTypeName;
    private final Set<String> extension;

    MimeType(String mimeTypeName, Set<String> extension) {
        this.mimeTypeName = mimeTypeName;
        this.extension = extension;
    }

    public Set<String> getExtension() {
        return extension;
    }

    public String getSuffix() {
        return new ArrayList<>(extension).get(0);
    }

    public static Set<MimeType> ofAll() {
        return EnumSet.allOf(MimeType.class);
    }

    public static Set<MimeType> of(MimeType type, MimeType... mimeTypes) {
        return EnumSet.of(type, mimeTypes);
    }

    public static Set<MimeType> ofImage() {
        return EnumSet.of(JPEG, PNG, GIF, BMP, WEBP);
    }

    public static Set<MimeType> ofVideo() {
        return EnumSet.of(MPEG, MP4, QUICKTIME, THREEGPP, MKV, WEBM, TS, AVI);
    }

    public static boolean isImage(String mimeType) {
        if (mimeType == null) return false;
        return mimeType.startsWith("image");
    }

    public static boolean isVideo(String mimeType) {
        if (mimeType == null) return false;
        return mimeType.startsWith("video");
    }

    public static boolean isGif(String mimieType) {
        if (mimieType == null) return false;
        return mimieType.equals(MimeType.GIF.toString());
    }

    @Override
    public String toString() {
        return mimeTypeName;
    }

    private static Set<String> arraySetOf(String... suffixes) {
        return new ArraySet<>(Arrays.asList(suffixes));
    }

    /**
     *
     * @param mimeTypes 要选组的自定义类型
     * @return 查询中实际对应的文件类型
     */
    public static ArrayList<String> getMimeTypeList(Set<MimeType> mimeTypes) {

        if (mimeTypes == null) return new ArrayList<>();
        ArrayList<String> mimeList = new ArrayList<>();
        for (MimeType mimeType : mimeTypes) {
            if (mimeType.extension != null) {
                for (String s : mimeType.extension) {
                    if (MimeType.isImage(String.valueOf(mimeType))) {
                        mimeList.add("image/" + s);
                    } else if (MimeType.isVideo(String.valueOf(mimeType))) {
                        mimeList.add("video/" + s);
                    }
                }
            }
        }
        return mimeList;
    }


}
